package com.shanjupay.merchant.controller;

import com.shanjupay.common.exception.BcException;
import com.shanjupay.common.web.BaseController;
import com.shanjupay.common.web.BaseResponse;
import com.shanjupay.merchant.api.MerchantService;
import com.shanjupay.merchant.api.model.MerchantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaoman
 * @version 1.0
 * @date 2021/6/20 20:06
 */

@RestController
@Api(tags = "商户相关接口")
@Slf4j
public class MerchantController extends BaseController {
    @Reference
    private MerchantService merchantService;


    @ApiOperation("根据id查询商户信息")
    @GetMapping("/merchants")
    public BaseResponse<MerchantVO> queryMerchantById(@ApiParam(value = "商户Id", name = "id") @RequestParam("id") Long id) {

        BaseResponse restResponse;
        try {
            MerchantVO result = merchantService.findMerchantById(id);
            restResponse = BaseResponse.success("查询成功!", result);
        } catch (BcException e) {
            restResponse = BaseResponse.error(e.getCode(), e.getMessage());
            log.error(e.getMessage());
        }
        return restResponse;
    }

    @ApiOperation("发送验证码注册商户")
    @GetMapping("/sendSms")
    public BaseResponse<String> sendSms(@RequestParam("phone") String phone) {
        BaseResponse<String> restResponse;
        try {
            String result = merchantService.sendSms(phone);
            restResponse = BaseResponse.success("验证码发送成功!", result);
        } catch (BcException e) {
            restResponse = BaseResponse.error(e.getCode(), e.getMessage());
            log.error(e.getMessage());
        }
        return restResponse;
    }


}
